package handler

import (
	"high-one-hw1/model"

	"github.com/gin-gonic/gin"
)

// 门店添加

func ShopAdd(c *gin.Context) {
	shop_code := c.PostForm("shop_code")
	shop_name := c.PostForm("shop_name")
	shop_address := c.PostForm("shop_address")

	shop := model.Shop{
		ShopCode:    shop_code,
		ShopName:    shop_name,
		ShopAddress: shop_address,
	}
	err := model.DB.Create(&shop).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

// 门店列表

func ShopList(c *gin.Context) {
	var shop []model.Shop
	err := model.DB.Debug().Find(&shop).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "列表获取失败",
		})
		return
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "列表获取成功",
		"data": shop,
	})
	return
}

// 修改门店

func ShopDetail(c *gin.Context) {
	id := c.PostForm("id")
	shop_code := c.PostForm("shop_code")
	shop_name := c.PostForm("shop_name")
	shop_address := c.PostForm("shop_address")

	shop := model.Shop{
		ShopCode:    shop_code,
		ShopName:    shop_name,
		ShopAddress: shop_address,
	}
	err := model.DB.Model(&model.Shop{}).Where("id=?", id).Updates(&shop).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "修改失败",
		})
		return
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "修改成功",
	})
	return
}
